﻿/*
	Copyright (C) 2011-2012 Bernardo van der Wal <bernardo@thebigb.nl>

	Permission is hereby granted, free of charge, to any person obtaining a copy of
	this software and associated documentation files (the "Software"), to deal in
	the Software without restriction, including without limitation the rights to
	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
	of the Software, and to permit persons to whom the Software is furnished to do
	so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
using System;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using TheBigB.JsonSerializerLite;
using fastJSON;

namespace Benchmark
{
	/// 
	/// JsonSerializerLite vs. fastJSON benchmark
	/// 
	/// Side by side comparison of JsonSerializerLite vs. Mehdi Gholam's
	/// fastJSON.
	/// 
	/// Currently JsonSerializerLite is outperformed by fastJSON. The goal is
	/// to first finish this project functionality-wise, and eventually to
	/// perform comparable to or better than fastJSON.
	/// 
	/// fastJSON can be found on CodePlex: http://fastjson.codeplex.com/
	/// 

	class LibraryComparison
	{
		const int REPETITIONS = 1000;

		public static TestObject testObject;
		public static string testString;

		static void Main(string[] args)
		{
			Random rand = new Random();
			IEnumerable<long> result;

			testObject = new TestObject();
			testObject.testIntArray = new int[] { 1, 2, 3, 4 };
			testObject.testByte = 123;
			testObject.testShort = 1234;
			testObject.testInt = 12345;
			testObject.testLong = 123456;
			testObject.testFloat = 123.456f;
			testObject.testDouble = 1234.5678;
			testObject.testString = "Test";
			testObject.embeddedObject = new ChildObject("Child");

			testString = "{\"testByte\":123,\"testShort\":1234,\"testInt\":12345,\"testLong\":123456,\"testFloat\":123.456,\"testDouble\":1234.5678,\"testString\":\"Test\",\"embeddedObject\":{\"testString\":\"Child\"}}";

			Console.WriteLine("===========================================");
			Console.WriteLine("JsonSerializerLite");
			Console.WriteLine("===========================================");

			result = Benchmark.This(benchSerialize_JsonSerializerLite).Take(500);
	
			Console.WriteLine("[Serialize]");
			Console.WriteLine(String.Format("Average:\t{0} ticks", result.Average()));
			Console.WriteLine(String.Format("Median:\t\t{0} ticks", result.Median()));
			Console.WriteLine(String.Format("Minimum:\t{0} ticks", result.Min()));
			Console.WriteLine(String.Format("Maximum:\t{0} ticks", result.Max()));
			Console.WriteLine();

			result = Benchmark.This(benchDeserialize_JsonSerializerLite).Take(500);

			Console.WriteLine("[Deserialize]");
			Console.WriteLine(String.Format("Average:\t{0} ticks", result.Average()));
			Console.WriteLine(String.Format("Median:\t\t{0} ticks", result.Median()));
			Console.WriteLine(String.Format("Minimum:\t{0} ticks", result.Min()));
			Console.WriteLine(String.Format("Maximum:\t{0} ticks", result.Max()));
			Console.WriteLine();

			Console.WriteLine("===========================================");
			Console.WriteLine("fastJSON");
			Console.WriteLine("===========================================");

			result = Benchmark.This(benchSerialize_fastJSON).Take(500);

			Console.WriteLine("[Serialize]");
			Console.WriteLine(String.Format("Average:\t{0} ticks", result.Average()));
			Console.WriteLine(String.Format("Median:\t\t{0} ticks", result.Median()));
			Console.WriteLine(String.Format("Minimum:\t{0} ticks", result.Min()));
			Console.WriteLine(String.Format("Maximum:\t{0} ticks", result.Max()));
			Console.WriteLine();

			result = Benchmark.This(benchDeserialize_fastJSON).Take(500);

			Console.WriteLine("[Deserialize]");
			Console.WriteLine(String.Format("Average:\t{0} ticks", result.Average()));
			Console.WriteLine(String.Format("Median:\t\t{0} ticks", result.Median()));
			Console.WriteLine(String.Format("Minimum:\t{0} ticks", result.Min()));
			Console.WriteLine(String.Format("Maximum:\t{0} ticks", result.Max()));
			Console.ReadLine();
			
			Console.ReadLine();
		}

		public static void benchSerialize_JsonSerializerLite()
		{
			string result = JsonSerializer.Default.Serialize(testObject);
		}

		public static void benchSerialize_fastJSON()
		{
			string result = fastJSON.JSON.Instance.ToJSON(testObject);
		}

		public static void benchDeserialize_JsonSerializerLite()
		{
			TestObject result = JsonSerializer.Default.Deserialize<TestObject>(testString);
		}

		public static void benchDeserialize_fastJSON()
		{
			TestObject result = fastJSON.JSON.Instance.ToObject<TestObject>(testString);
		}
	}

	public class TestObject
	{
		public int[] testIntArray { get; set; }
		public byte testByte { get; set; }
		public short testShort { get; set; }
		public int testInt { get; set; }
		public long testLong { get; set; }
		public float testFloat { get; set; }
		public double testDouble { get; set; }
		public string testString { get; set; }
		public ChildObject embeddedObject { get; set; }
	}

	public class ChildObject
	{
		public string testString { get; set; }

		public ChildObject()
		{

		}

		public ChildObject(string str)
		{
			this.testString = str;
		}
	}
}
